import axios from 'axios'

/**
 * 导出Excel公共方法
 * @param url
 * @param param 参数
 */
export function exportExcel(url, param) {
  axios({
    method: 'post',
    url: `${process.env.VUE_APP_DEV_API}${url}`,
    responseType: 'blob',
    headers: {
      'Content-Type': 'application/json',
      'accept': 'application/octet-stream'
      // ....自定义header
    },
    data: param,
    param: param
  })
    .then(response => {
      const data = response.data
      // const xhr = response.request
      // const headers = xhr.getAllResponseHeaders()
      // console.log('headers' + headers)
      // const contentDisposition = xhr.getResponseHeader('Content-Disposition')
      // 跨域情况下，不从后端获取Content-Disposition，前台手动拼接文件名
      // const contentDisposition = xhr.getResponseHeader('Cache-Control')
      const url = URL.createObjectURL(data)
      // const fileName = decodeURI(contentDisposition.split('filename=')[1].split('filename*=')[0])
      const fileName = param.fileName
      const link = document.createElement('a')
      link.href = url
      link.download = fileName
      link.click()
      link.remove()
    })
    .catch(() => {
      // getLog(logData, 1)//调用日志接口
    })
}
